﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ZSZ.Common
{
    /// <summary>
    /// 发送短信
    /// </summary>
    public class CommonSMSSender
    {
        public string UserName { get; set; }
        public String AppKey { get; set; }
        public CommonSMSResult SendSMS(string templateId, string code, string phoneNum)
        {
            WebClient wc = new WebClient();
            string url = "http://sms.rupeng.cn/SendSms.ashx?userName=" + Uri.EscapeDataString(UserName)
            + "&appKey=" + Uri.EscapeDataString(AppKey)
            + "&templateId=" + Uri.EscapeDataString(templateId)
            + "&code=" + Uri.EscapeDataString(code)
            + "&phoneNum=" + Uri.EscapeDataString(phoneNum);
            wc.Encoding = Encoding.UTF8;
            string resp = wc.DownloadString(url);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            CommonSMSResult result = jss.Deserialize<CommonSMSResult>(resp);
            return result;
        }
    }

    #region 调用 CommonSMSSender
    //调用 CommonSMSSender
    //CommonSMSSender smsSender = new CommonSMSSender();
    //smsSender.UserName = userName;
    //smsSender.AppKey = appKey;
    //var result = smsSender.SendSMS(templateId, code, phoneNum);
    //Console.WriteLine("返回码：" + result.code + "，返回消息：" + result.msg); 
    #endregion
}
